<?php
/**
 * 幻灯片管理
 */

namespace backend\controllers;


use common\models\Banner;
use common\models\BannerItem;

class BannerController extends CommonController
{
    public function actionIndex()
    {
        $this->setPageSize(0);
        $this->setQuery(Banner::find());
        return $this->query();
    }

    public function actionCreate()
    {
        $model = new Banner();
        $this->setModel($model);
        return $this->save();
    }

    public function actionUpdate($id)
    {
        $model = Banner::findOne($id);
        $this->setModel($model);
        return $this->save();
    }

    public function actionDelete()
    {
        $this->setModel(new Banner());
        $this->delete();
    }

    public function actionItemIndex($banner_id)
    {
        $this->setPageSize(0);
        $this->setQuery(BannerItem::find()->where("banner_id=$banner_id")->orderBy(['sort' => SORT_ASC]));
        $banner = Banner::findOne($banner_id);
        $this->setParams(['banner' => $banner]);
        $this->setXView('item-index');
        return $this->query();
    }

    public function actionItemCreate($banner_id)
    {
        $model = new BannerItem();
        $model->banner_id = $banner_id;
        $model->sort = 255;
        $this->setModel($model);
        $this->setXView('item-form');
        $this->setUrl(['item-index', 'banner_id' => $banner_id]);
        return $this->save();
    }

    public function actionItemUpdate($id)
    {
        $model = BannerItem::findOne($id);
        $this->setModel($model);
        $this->setXView('item-form');
        $this->setUrl(['item-index', 'banner_id' => $model->banner_id]);
        return $this->save();
    }

    public function actionItemDelete($banner_id)
    {
        $this->setModel(new BannerItem());
        $this->setUrl(['item-index', 'banner_id' => $banner_id]);
        $this->delete();
    }
}